figure.Mezcladora = new Class({
    Extends: figure.Bascula,

    initialize: function(id, css, value, unit, min, capacity, step, mainPercentage) {
        this.parent(id, css, value, unit, min, capacity, step);
        this.mainPercentage = mainPercentage;
        this.mainComponentAmount = 0;
        this.secondaryComponentAmount = 0;
    },

    addEvents: function() {
        var controlElement = this.controlElement;
        controlElement.on('growUp', function growUp(event, nextClasses, previousClasses, mezcladora) {
            if (figure.stopAnimation) {
                window.setTimeout(function() { growUp(event, nextClasses, previousClasses, mezcladora) }, figure.speed);
                return;
            }
            var valueAsInt = parseInt(controlElement[0].value);
            var maxValueAsInt = parseInt(controlElement[0].max);
            var i, delta, deltaMain, deltaSecondary, amountEntriesOpen = 0, amountOutputsOpen = 0;
            if (valueAsInt < 0.8*maxValueAsInt) {
                if (mezcladora.mainComponentAmount < mezcladora.mainPercentage*maxValueAsInt) {
                    figure.pipesStatus[previousClasses[0]+'End'] = true;
                    if (figure.isOpen(previousClasses[0])) {
                        amountEntriesOpen++;
                    }
                }
                if (mezcladora.secondaryComponentAmount < (1-mezcladora.mainPercentage)*maxValueAsInt) {
                    figure.pipesStatus[previousClasses[1]+'End'] = true;
                    if (figure.isOpen(previousClasses[1])) {
                        amountEntriesOpen++;
                    }
                }
                if (mezcladora.mainComponentAmount < mezcladora.mainPercentage*maxValueAsInt) {
                    if (figure.isOpen(previousClasses[0])) {
                        delta = 500/amountEntriesOpen;
                        valueAsInt += delta;
                        mezcladora.mainComponentAmount += delta;
                        controlElement[0].value = valueAsInt < maxValueAsInt ? valueAsInt: maxValueAsInt;
                        controlElement.trigger('change');
                    }
                } else {
                    figure.pipesStatus[previousClasses[0]+'End'] = false;
                }
                if (mezcladora.secondaryComponentAmount < (1-mezcladora.mainPercentage)*maxValueAsInt) {
                    if (figure.isOpen(previousClasses[1])) {
                        delta = 500/amountEntriesOpen;
                        valueAsInt += delta;
                        mezcladora.secondaryComponentAmount += delta;
                        controlElement[0].value = valueAsInt < maxValueAsInt ? valueAsInt: maxValueAsInt;
                        controlElement.trigger('change');
                    }
                } else {
                    figure.pipesStatus[previousClasses[1]+'End'] = false;
                }
                for (i = 0; i< nextClasses.length; i++){
                    figure.pipesStatus[nextClasses[i] + 'Start'] = false;
                }
            } else if (valueAsInt < 0.9*maxValueAsInt) {
                if (mezcladora.mainComponentAmount < mezcladora.mainPercentage*maxValueAsInt) {
                    figure.pipesStatus[previousClasses[0]+'End'] = true;
                    if (figure.isOpen(previousClasses[0])) {
                        amountEntriesOpen++;
                    }
                }
                if (mezcladora.secondaryComponentAmount < (1-mezcladora.mainPercentage)*maxValueAsInt) {
                    figure.pipesStatus[previousClasses[1]+'End'] = true;
                    if (figure.isOpen(previousClasses[1])) {
                        amountEntriesOpen++;
                    }
                }
                if (mezcladora.mainComponentAmount < mezcladora.mainPercentage*maxValueAsInt) {
                    if (figure.isOpen(previousClasses[0])) {
                        delta = 500/amountEntriesOpen;
                        valueAsInt += delta;
                        mezcladora.mainComponentAmount += delta;
                        controlElement[0].value = valueAsInt < maxValueAsInt ? valueAsInt: maxValueAsInt;
                        controlElement.trigger('change');
                    }
                } else {
                    figure.pipesStatus[previousClasses[0]+'End'] = false;
                }
                if (mezcladora.secondaryComponentAmount < (1-mezcladora.mainPercentage)*maxValueAsInt) {
                    if (figure.isOpen(previousClasses[1])) {
                        delta = 500/amountEntriesOpen;
                        valueAsInt += delta;
                        mezcladora.secondaryComponentAmount += delta;
                        controlElement[0].value = valueAsInt < maxValueAsInt ? valueAsInt: maxValueAsInt;
                        controlElement.trigger('change');
                    }
                } else {
                    figure.pipesStatus[previousClasses[1]+'End'] = false;
                }
                for (i = 0; i< nextClasses.length; i++){
                    figure.pipesStatus[nextClasses[i]+'Start'] = true;
                    if (figure.isOpen(nextClasses[i])) {
                        amountOutputsOpen++;
                    }
                }
                for (i = 0; i< nextClasses.length; i++){
                    if (figure.isOpen(nextClasses[i])) {
                        delta = 500/amountOutputsOpen;
                        deltaMain = delta*mezcladora.mainComponentAmount/(mezcladora.mainComponentAmount+mezcladora.secondaryComponentAmount);
                        deltaSecondary = delta*mezcladora.secondaryComponentAmount/(mezcladora.mainComponentAmount+mezcladora.secondaryComponentAmount);
                        mezcladora.mainComponentAmount = (mezcladora.mainComponentAmount - deltaMain > 0 ? mezcladora.mainComponentAmount - deltaMain : 0);
                        mezcladora.secondaryComponentAmount = (mezcladora.secondaryComponentAmount - deltaSecondary > 0 ? mezcladora.secondaryComponentAmount - deltaSecondary : 0);
                        valueAsInt = (valueAsInt - Math.min(deltaMain,mezcladora.mainComponentAmount) - Math.min(deltaSecondary,mezcladora.secondaryComponentAmount) > 0 ? valueAsInt - Math.min(deltaMain,mezcladora.mainComponentAmount) - Math.min(deltaSecondary,mezcladora.secondaryComponentAmount) : 0);
                        controlElement[0].value = valueAsInt;
                        controlElement.trigger('change');
                    }
                }
            } else {
                for (i = 0; i< previousClasses.length; i++){
                    figure.pipesStatus[previousClasses[i]+'End'] = false;
                }
                for (i = 0; i< nextClasses.length; i++){
                    figure.pipesStatus[nextClasses[i]+'Start'] = true;
                    if (figure.isOpen(nextClasses[i])) {
                        amountOutputsOpen++;
                    }
                }
                for (i = 0; i< nextClasses.length; i++){
                    if (figure.isOpen(nextClasses[i])) {
                        delta = 500/amountOutputsOpen;
                        deltaMain = delta*mezcladora.mainComponentAmount/(mezcladora.mainComponentAmount+mezcladora.secondaryComponentAmount);
                        deltaSecondary = delta*mezcladora.secondaryComponentAmount/(mezcladora.mainComponentAmount+mezcladora.secondaryComponentAmount);
                        mezcladora.mainComponentAmount = (mezcladora.mainComponentAmount - deltaMain > 0 ? mezcladora.mainComponentAmount - deltaMain : 0);
                        mezcladora.secondaryComponentAmount = (mezcladora.secondaryComponentAmount - deltaSecondary > 0 ? mezcladora.secondaryComponentAmount - deltaSecondary : 0);
                        valueAsInt = (valueAsInt - Math.min(deltaMain,mezcladora.mainComponentAmount) - Math.min(deltaSecondary,mezcladora.secondaryComponentAmount) > 0 ? valueAsInt - Math.min(deltaMain,mezcladora.mainComponentAmount) - Math.min(deltaSecondary,mezcladora.secondaryComponentAmount) : 0);
                        controlElement[0].value = valueAsInt;
                        controlElement.trigger('change');
                    }
                }
            }
            window.setTimeout(function() { growUp(event, nextClasses, previousClasses, mezcladora) }, figure.speed);
        });
    }

});
